package com.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crm.domain.Business;
import com.crm.dto.BaseStatisticsDTO;
import com.crm.dto.SalesStatisticDTO;
import com.crm.mapper.BusinessMapper;
import com.crm.mapper.ClueMapper;
import com.crm.mapper.ClueRecordMapper;
import com.crm.service.HomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private ClueRecordMapper clueRecordMapper;
    @Override
    public void seleteMethod(BaseStatisticsDTO statisticsDTO) {
        Long clue = clueMapper.selectCount(null);
        statisticsDTO.setCluesNum(clue);

        Long business = businessMapper.selectCount(null);
        statisticsDTO.setBusinessNum(business);

        Long record = clueRecordMapper.selectCount(null);
        statisticsDTO.setContractNum(record);

        QueryWrapper<Business> wrapper = new QueryWrapper<>();
        wrapper.select("SUM(amount) AS totalAmount");

        Map<String, Object> map = businessMapper.selectMaps(wrapper).get(0);
        BigDecimal totalAmount = map.get("totalAmount") != null ? (BigDecimal) map.get("totalAmount") : BigDecimal.ZERO;

        statisticsDTO.setSalesAmount(totalAmount);

    }

    @Override
    public List<SalesStatisticDTO> getBusinessChangeCount(LocalDateTime beginDateTime, LocalDateTime endDateTime) {
        return businessMapper.getBusinessChangeCount(beginDateTime, endDateTime);
    }

//    @Override
//    public List<SalesStatisticDTO> indexCount(LocalDateTime beginDateTime, LocalDateTime endDateTime) {
//        return businessMapper.indexCount(beginDateTime, endDateTime);
//    }


}
